package com.tmall.testgen.client.commond.impl;

import java.io.File;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.tmall.testgen.client.ClientDataCenter;
import com.tmall.testgen.client.commond.SendCommond;
import com.tmall.testgen.client.socket.SocketClient;
import com.tmall.testgen.common.bo.interact.apply.ApplyCommond;
import com.tmall.testgen.common.bo.interact.back.InterceptInfo;
import com.tmall.testgen.common.bo.interact.back.TransferCarFromClient;
import com.tmall.testgen.common.bo.interact.back.syn.ConnectionSuccResult;
import com.tmall.testgen.common.bo.interact.back.syn.SendCommonResult;
import com.tmall.testgen.common.socket.syn.SynCallBack;
import com.tmall.testgen.common.socket.syn.SynObject;
import com.tmall.testgen.common.util.VMUtil;
import com.tmall.testgen.plugin.ConsoleFactory;
import com.tmall.testgen.server.socket.CommondEnum;
import com.tmall.testgen.transfer.client.TransferClient;

/**
 * @author haojia
 * @date 2014-07-04
 */
public class SendCommondImpl implements SendCommond {

	public Set<String> local=new HashSet<String>();
	{
		local.add("localhost");
		local.add("127.0.0.1");
	}
	
	
	
	@Override
	public ConnectionSuccResult tryAttatch( String ip,String pid) throws Exception {
	    if(!TransferClient.isConnectedServer()) {
            TransferClient.startConnectServer(ip);
	    }
	    
	    
	   TransferClient.invokeVM(pid);
	   return SocketClient.getInstance().startConnectServer(ip);
	}
	

	
	@Override
	public SendCommonResult sendCommond(final CommondEnum commond) {
		ApplyCommond applyCommond=new ApplyCommond();
		applyCommond.setCommond(commond);
		applyCommond.setUserId(ClientDataCenter.getInstance().getUserId());
		return	(SendCommonResult) new SynCallBack(){
			@Override
			public Object handleReceive(SynObject synObj) {
				if( (synObj instanceof SendCommonResult)&&(commond.getCommondCode()==((SendCommonResult)synObj).getCommond().getCommondCode())){
					return synObj;
				}
				return null;
			}
			
		}.sendObj(new TransferCarFromClient(TransferCarFromClient.TYPE_APPLY_COMMOND, applyCommond),SocketClient.getInstance().getOut());
		
	}

	@Override
	public List<InterceptInfo> refrehInterceptInfo() {
		return null;
	}

	@Override
	public boolean changeInterceptCond(String cond) {
		return false;
	}

	@Override
	public boolean changeInerceptMethodCond(String cond) {
		return false;
	}

//	@Override
//	public Map<String, String> getLocalVMs() throws Exception {
//		return VMUtil.getLocalVMs();
//	}

	@Override
	public Map<String, String> getRemoteVMs(String serverIp) throws Exception {
	    if(!TransferClient.isConnectedServer()) 
	        TransferClient.startConnectServer(serverIp);
		 return TransferClient.getGvms();
	}

}
